package com.darwin.darwinUI.manager
{
	import com.darwin.darwinUI.manager.base.PopUpTween;
	import com.darwin.darwinUI.manager.base.RemovePopUpTween;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.utils.Dictionary;
	
	public class PopUpManager extends EventDispatcher
	{
		public function PopUpManager(target:IEventDispatcher=null)
		{
			super(target);
			_stage = UIStage.uiStage;
			
			modalSprite = new Sprite();
			modalSprite.graphics.beginFill(PopUpManager.modalColor,PopUpManager.modalAlpha);
			modalSprite.graphics.drawRect(0,0,10,10);
			modalSprite.graphics.endFill();
			modalSprite.width = _stage.stageWidth;
			modalSprite.height = _stage.stageHeight;
			
			_stage.addEventListener(Event.RESIZE,stageResizeHandler);
			
			
			
			_modalDic = new Dictionary();
			_centerDic = new Dictionary();
			_coverDic = new Dictionary();
			_preVisibleDic = new Dictionary();
			_priorityDic = new Dictionary();
			_tweenDic = new Dictionary();
			_removeTweenDic = new Dictionary();
			_oldWindow = new Dictionary();
		}
		
		public static var modalColor:uint = 0x000000;
		public static var modalAlpha:Number = 0.5;
		
		
		//模态层
		protected var modalSprite:Sprite;
		private var _stage:Stage;
		
		
		
		
		/**
		 * 窗口大小改变时处理
		 */
		private function stageResizeHandler(e:Event):void
		{
			modalSprite.width = _stage.stageWidth;
			modalSprite.height = _stage.stageHeight;
			
			resizeHandler(_stage.stageWidth,_stage.stageHeight);
		}
		
		
		protected function resizeHandler(stageWidth:Number,stageHeight:Number):void
		{
			if(_currentWindow != null)
			{
				if(_centerDic[_currentWindow] != null)
				{
					_currentWindow.x = (stageWidth - _currentWindow.width)/2;
					_currentWindow.y = (stageHeight - _currentWindow.height)/2;
				}//if
			}//if
		}
		
		
		private var _parent:DisplayObjectContainer;
		/**
		 * 弹出对象所在的父容器，如果未设置过parent，默认使用stage
		 */
		public function get parent():DisplayObjectContainer
		{
			return _parent || _stage;
		}
		
		public function set parent(value:DisplayObjectContainer):void
		{
			_parent = value;
		}
		
		
		private var _popUpTween:PopUpTween;
		/**
		 * 弹出框动画类
		 */
		public function get popUpTween():PopUpTween
		{
			if(_popUpTween == null)
			{
				_popUpTween = new PopUpTween();
			}
			return _popUpTween;
		}

		public function set popUpTween(value:PopUpTween):void
		{
			_popUpTween = value;
		}
		
		
		private var _removeTween:RemovePopUpTween;

		/**
		 * 移除窗口的动画类
		 */
		public function get removeTween():RemovePopUpTween
		{
			if(_removeTween == null)
			{
				_removeTween = new RemovePopUpTween();
			}
			return _removeTween;
		}

		public function set removeTween(value:RemovePopUpTween):void
		{
			_removeTween = value;
		}
		
		
		private var _hideTween:RemovePopUpTween;

		/**
		 * 当窗口因为其它窗口弹出而需要隐藏时使用RemovePopUpTween实列移除
		 * @return 
		 */
		public function get hideTween():RemovePopUpTween
		{
			if(_hideTween == null)
			{
				_hideTween = new RemovePopUpTween();
			}
			return _hideTween;
		}

		public function set hideTween(value:RemovePopUpTween):void
		{
			_hideTween = value;
		}
		
		
		private var _showTween:PopUpTween;

		/**
		 * 显示窗口
		 */
		public function get showTween():PopUpTween
		{
			if(_showTween == null)
			{
				_showTween = new PopUpTween();
			}
			return _showTween;
		}

		public function set showTween(value:PopUpTween):void
		{
			_showTween = value;
		}
		
		
		
		//当前的窗口
		private var _currentWindow:DisplayObject;
		
		/**
		 * 取得当前弹出的，在顶层的窗口
		 */
		public function getCurrentWindow():DisplayObject
		{
			return _currentWindow;
		}
		
		private var _modalDic:Dictionary;
		private var _centerDic:Dictionary;
		private var _coverDic:Dictionary;
		private var _preVisibleDic:Dictionary;
		private var _priorityDic:Dictionary;
		private var _tweenDic:Dictionary;
		private var _removeTweenDic:Dictionary;
		
		
		private var _oldWindow:Dictionary;
		
		/**
		 * 添加一个弹出框
		 * @param displayObject 弹出框对象
		 * @param modal 是否模态
		 * @param locCenter 是否居中
		 * @param preVisible 当前已打开的窗口是否继续可见
		 * @param cover 如果已有弹窗，且优先级与要弹出的框相同是覆盖还是等待已有窗口关闭 true 为覆盖
		 * @param priority 弹框的优先级.
		 * 如果已弹出窗口的priority大于当前要弹出的窗口，则当前要弹出的窗口将在其它窗口关闭后显示
		 */
		public function addPopUp(window:DisplayObject,modal:Boolean = true,
								 locCenter:Boolean = true,cover:Boolean = true,preVisible:Boolean = false,
								 priority:int = 0,tween:PopUpTween = null,removeTween:RemovePopUpTween = null):void
		{
			if(modal)
			{
				if(preVisible)
				{
					parent.addChild(modalSprite);
				}else{
					parent.addChildAt(modalSprite,0);
				}
			}else{
				if(preVisible == false)
				{
					if(modalSprite.parent) parent.removeChild(modalSprite);
				}
			}
			
			_modalDic[window] = modal;
			_centerDic[window] = locCenter;
			_coverDic[window] = cover;
			_preVisibleDic[window] = preVisible;
			_priorityDic[window] = priority;
			_tweenDic[window] = tween;
			_removeTweenDic[window] = removeTween;
			
			
			if(_currentWindow != null)
			{
				var $cPriority:int = _priorityDic[_currentWindow];
				_oldWindow[_currentWindow] = _currentWindow;
				if($cPriority > priority)
				{
					return;
				}else if($cPriority < priority){
					if(_preVisibleDic[window] == false)
					{
						hideTween.tween(_currentWindow);
					}
				}else{
					if(!cover)
					{
						return;
					}else if(preVisible == false){
						hideTween.tween(_currentWindow);
					}
				}
			}
			_currentWindow = window;
			var $tween:PopUpTween = tween || popUpTween;
			var $x:Number;
			var $y:Number;
			if(locCenter)
			{
				$x = Math.round((_stage.stageWidth - window.width)/2);
				$y = Math.round((_stage.stageHeight - window.height)/2);
			}else{
				$x = Math.round(window.x);
				$y = Math.round(window.y);
			}
			$tween.start(window);
			parent.addChild(window);
			$tween.tween(window,$x,$y);
		}
		
		
		/**
		 * 移除一个弹出框
		 * @param displayObject
		 */
		public function removePopUp(window:DisplayObject):void
		{
			var $tween:RemovePopUpTween;
			if(_removeTweenDic[window] != null)
			{
				$tween = _removeTweenDic[window];
			}else{
				$tween = removeTween;
			}
			
			delete _modalDic[window];
			delete _centerDic[window];
			delete _coverDic[window];
			delete _preVisibleDic[window];
			delete _priorityDic[window];
			delete _tweenDic[window];
			delete _removeTweenDic[window];
			
			$tween.tween(window);
			
			if(_currentWindow == window)
			{
				_currentWindow = null;
				
				if(modalSprite.parent)
				{
					modalSprite.parent.removeChild(modalSprite);
				}
			
				var oldWindow:DisplayObject = getWindow();
			
				if(oldWindow != null)
				{
					var $showTween:PopUpTween;
					if(_oldWindow[oldWindow] != null)
					{
						$showTween = showTween;
					}else{
						$showTween = _tweenDic[oldWindow];
					}
					addPopUp(oldWindow,_modalDic[oldWindow],_centerDic[oldWindow],
						_coverDic[oldWindow],_preVisibleDic[oldWindow],
						_priorityDic[oldWindow],$showTween,_removeTweenDic[oldWindow]);
				}
			}
		}
		
		private function getWindow():DisplayObject
		{
			var $priority:int = -9999;
			var $returnItem:DisplayObject;
			for(var item:Object in _priorityDic)
			{
				if(_priorityDic[item] > $priority)
				{
					$priority = _priorityDic[item];
					$returnItem = item as DisplayObject;
				}
			}
			return $returnItem;
		}
		
		
		
	}
}
